﻿/*******************************************************************************
#                    ADMColumnBroker.cs
#  Comment:
#
#  Current Version: V1.0
#  Author: 
#
#  History List:
#  V1.0    Created by Onions@2009-8-21 13:08:14
#
#******************************************************************************/
#region Usings
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using SYS.Basic;

using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Collections;
using IBatisNet.Extend;
#endregion

namespace ExpertMIS.Brokers
{
    public class ADMColumnBroker : ExpertMISBaseBroker
    {
        #region Private Methods

        private object UpdateADMColumnEntity(IEntity entity)
        {
            return SqlMap.Update("UpdateADMColumnEntity", entity);
        }

        private object InsertADMColumnEntity(IEntity entity)
        {
            return SqlMap.Insert("InsertADMColumnEntity", entity);
        }

        private object DeleteADMColumnEntity(IEntity entity)
        {
            return SqlMap.Delete("DeleteADMColumnEntity", entity);
        }

        #endregion

        #region Override Methods

        protected override IEntity ProcessGetEntity(System.Collections.Hashtable keys)
        {
            if (!keys.Contains("OID")) throw new Exception("invalid request parameters.");
            return SqlMap.QueryForObject<IEntity>("SelectADMColumnEntityByOID", keys["OID"] as string);
        }

        protected override bool ProcessSaveEntity(IEntity entity, Hashtable request)
        {
            if (entity.EntityState == EntityState.New)
            {
                InsertADMColumnEntity(entity);
                entity.EntityState = EntityState.Old;
            }
            else
            {
                UpdateADMColumnEntity(entity);
            }

            return true;
        }

        protected override IEntity ProcessNewEntity()
        {
            ADMColumnEntity entity = new ADMColumnEntity();

            // initial entity object

            return entity;
        }

        protected override bool ProcessRemoveEntity(IEntity entity, Hashtable request)
        {
            try
            {
                DeleteADMColumnEntity(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// 获取所有栏目信息
        /// </summary>
        /// <returns></returns>
        public IList<ADMColumnEntity> GetAllColumns()
        {
            return SqlMap.QueryForList<ADMColumnEntity>("SelectAllColumns", null);
        }

        /// <summary>
        /// 获取一级栏目信息
        /// </summary>
        /// <param name="nMax">返回最大条数</param>
        /// <returns></returns>
        public IList<ADMColumnEntity> GetRootColumns(int nMax)
        {
            string selectColumns = "*";

            if (nMax > 0)
            {
                selectColumns = string.Format("Top {0} *",nMax.ToString());
            }

            return SqlMap.QueryForList<ADMColumnEntity>("SelectRootColumns", selectColumns);
        }

        public ADMColumnEntity GetColumnEntityByOID(string columnOID)
        {
            Hashtable htKeys = new Hashtable();
            htKeys["OID"] = columnOID;

            return GetEntity(htKeys) as ADMColumnEntity;
        }

        public ADMColumnEntity GetColumnByArtileOID(string ArticleOID)
        {
            return SqlMap.QueryForObject<ADMColumnEntity>("GetColumnByArtileOID", ArticleOID);
        }
        #endregion
    }
}